# 电商用户兴趣匹配与商品推荐系统

def main():
    # 模拟数据
    # 商品数据库，每个商品有一个唯一的ID和一些标签（集合）
    products = {
        1: {"name": "笔记本电脑", "tags": {"电子", "办公", "学习"}},
        2: {"name": "智能手机", "tags": {"电子", "通讯", "娱乐"}},
        3: {"name": "无线耳机", "tags": {"电子", "音频", "配件"}},
        4: {"name": "运动手表", "tags": {"运动", "健康", "配件"}},
        5: {"name": "跑步鞋", "tags": {"运动", "户外", "服装"}},
        6: {"name": "瑜伽垫", "tags": {"运动", "健身", "家居"}},
        7: {"name": "咖啡机", "tags": {"家居", "厨房", "电器"}},
        8: {"name": "电水壶", "tags": {"家居", "厨房", "电器"}},
        9: {"name": "洗发水", "tags": {"美妆", "个人护理"}},
        10: {"name": "护发素", "tags": {"美妆", "个人护理"}}
    }

    # 用户数据，包含浏览历史（集合）和购买历史（集合）
    user_data = {
        "Alice": {"viewed": {1, 2}, "purchased": {2}},
        "Bob": {"viewed": {3, 4}, "purchased": {4}},
        "Charlie": {"viewed": {5, 6}, "purchased": {5}},
        "David": {"viewed": {7, 8}, "purchased": {7}}
    }

    # 当前用户
    current_user = "Alice"

    # 推荐系统
    while True:
        print("\n电商推荐系统")
        print("1. 查看浏览历史")
        print("2. 查看购买历史")
        print("3. 根据浏览历史推荐商品")
        print("4. 根据购买历史推荐商品")
        print("5. 根据其他用户兴趣推荐商品")
        print("6. 退出")

        choice = input("请输入您的选择（1-6）：")

        if choice == "1":
            # 查看浏览历史
            viewed_products = user_data[current_user]["viewed"]
            print(f"\n{current_user} 的浏览历史：")
            for pid in viewed_products:
                print(f"商品ID: {pid}, 名称: {products[pid]['name']}")

        elif choice == "2":
            # 查看购买历史
            purchased_products = user_data[current_user]["purchased"]
            print(f"\n{current_user} 的购买历史：")
            for pid in purchased_products:
                print(f"商品ID: {pid}, 名称: {products[pid]['name']}")

        elif choice == "3":
            # 根据浏览历史推荐商品
            viewed_tags = set()
            for pid in user_data[current_user]["viewed"]:
                viewed_tags.update(products[pid]["tags"])  # 合并所有浏览商品的标签

            recommended_products = []
            for pid, product in products.items():
                # 找出标签与浏览历史有交集的商品
                if product["tags"] & viewed_tags and pid not in user_data[current_user]["viewed"]:
                    recommended_products.append((pid, product["name"]))

            print(f"\n根据 {current_user} 的浏览历史推荐的商品：")
            for pid, name in recommended_products:
                print(f"商品ID: {pid}, 名称: {name}")

        elif choice == "4":
            # 根据购买历史推荐商品
            purchased_tags = set()
            for pid in user_data[current_user]["purchased"]:
                purchased_tags.update(products[pid]["tags"])  # 合并所有购买商品的标签

            recommended_products = []
            for pid, product in products.items():
                # 找出标签与购买历史有交集的商品
                if product["tags"] & purchased_tags and pid not in user_data[current_user]["purchased"]:
                    recommended_products.append((pid, product["name"]))

            print(f"\n根据 {current_user} 的购买历史推荐的商品：")
            for pid, name in recommended_products:
                print(f"商品ID: {pid}, 名称: {name}")

        elif choice == "5":
            # 根据其他用户兴趣推荐商品
            # 找出与其他用户浏览历史的交集较大的商品
            all_viewed = set()
            for user, data in user_data.items():
                if user != current_user:
                    all_viewed.update(data["viewed"])  # 合并其他用户的浏览历史

            # 找出当前用户未浏览但其他用户浏览过的商品
            recommended_products = all_viewed - user_data[current_user]["viewed"]

            print(f"\n根据其他用户的兴趣推荐的商品：")
            for pid in recommended_products:
                print(f"商品ID: {pid}, 名称: {products[pid]['name']}")

        elif choice == "6":
            # 退出系统
            print("感谢使用电商推荐系统，再见！")
            break

        else:
            print("无效的选择，请重新输入！")


if __name__ == "__main__":
    main()